<template>
	<view class="index">
		<view class="customer-wrapper">
			<view class="customer-info">
				<view class="item">
					客户名称：{{ form.customerPetName }}
				</view>
				<view class="item">
					联系电话：{{ form.mobilePhone }}
				</view>
				<view class="item">
					<text v-if="form.status == 3">交付时间：{{ form.deliverTime.substring(0,16) }}</text>
					<text v-else-if="form.status == 2">送达时间：{{ form.mayDeliverDate.substring(0,10) }}</text>
					<text v-else>计划送达：{{ form.mayDeliverDate.substring(0,10) }}</text>
				</view>
			</view>
			<view class="storage-number">
				<view class="code">
					客户号：{{ form.sortCode || '--' }}
				</view>
				<view class="date">
					{{ form.supplyDate.substring(5,10) }} {{ form.schemaCaption }}
				</view>
			</view>
		</view>
		<view class="form-wrapper">
			<view class="title-wrapper">
				<view class="title">
					配送信息
				</view>
				<view class="edit" @click="goEdit">
					编辑
				</view>
			</view>
			<view class="item">
				<view class="label">
					电话号码：
				</view>
				<view class="value" @click="callphone">
					{{ form.phone }}
				</view>
			</view>
			<view class="item">
				<view class="label">
					车牌号：
				</view>
				<view class="value">
					{{ form.vehicleNo || '-' }}
				</view>
			</view>
			<view class="item">
				<view class="label">
					备注：
				</view>
				<view class="value">
					{{ form.remark || '-' }}
				</view>
			</view>
		</view>
		<view class="product-item" v-for="(item,index) in form.orderItems" :key="index">
			<view class="product-name">
				<view class="line"></view>
				<view class="name">
					{{ item.productName }}
				</view>
			</view>
			<view class="product-info">
				<view class="item">
					毛重：{{ item.grossWeight }}斤
				</view>
				<view class="item">
					净重：{{ item.netWeight }}斤
				</view>
				<view class="item">
					货号：{{ item.productCode }}
				</view>
				<view class="item">
					订货量：{{ item.quantity }} 件
				</view>
			</view>
			<view class="product-number">
				<view class="label">
					实配数量
				</view>
				<input type="number" v-model="item.actualQuantity" placeholder="输入实配数量" class="input" />
				<view class="unit">
					件
				</view>
			</view>
			<view class="tip" v-if="item.actualQuantity && item.actualQuantity < item.quantity">
				<uni-icons type="info-filled" color="#F26022" size="20"></uni-icons>
				<text>缺货{{ item.quantity - item.actualQuantity }}件：收货后由城市仓发起售后为客户退款</text>
			</view>
		</view>
		<view class="btn-wrapper">
			<view class="btn default" @click="goback">
				取消
			</view>
			<view class="btn" @click="submit">
				确认收货
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				code: ""
			}
		},
		onLoad(option) {
			this.getInfo(option.id)
		},
		computed: {

		},
		methods: {
			async getInfo(id) {
				const data = await this.$api.get('/app/deliver/order/getInfo', {
					id: id
				})
				// data.data.orderItems.forEach(item => {
				// 	item.actualQuantity = item.quantity;
				// })
				console.log(data.data)
				this.form = data.data;
			},
			goback() {
				uni.navigateBack()
			},
			close() {
				this.$refs.popup.close()
			},
			goEdit() {
				let params = {
					phone: this.form.phone,
					vehicleNo: this.form.vehicleNo,
					remark: this.form.remark
				}
				let encodedParam = JSON.stringify(params);
				uni.navigateTo({
					url: '/subPage_pickup/logistics?id=' + this.form.id + "&params=" + encodedParam
				})
			},
			callphone() {
				uni.makePhoneCall({
					phoneNumber: this.form.phone
				})
			},
			submit() {
				let msg = "";
				const array = this.form.orderItems;
				array.forEach(item => {
					if (!msg && !item.actualQuantity && item.actualQuantity != '0') {
						msg = `${item.productName}的实配数量不能为空`;
					} else if (!msg && parseFloat(item.actualQuantity) > parseFloat(item.quantity)) {
						msg = `${item.productName}的实配数量不能大于订货数量`;
					}
				});
				if (msg) {
					uni.showToast({
						title: msg,
						icon: 'none',
						duration: 2000
					})
					return
				}
				this.confirm()
			},
			async confirm() {
				const data = await this.$api.post('/app/deliver/order/delivery', {
					id: this.form.id,
					deliveryType: 'ALL',
					deliveryItems: this.form.orderItems.map(item => {
						return {
							id: item.id,
							actualQuantity: item.actualQuantity,
							quantity: item.quantity
						}
					})
				})
				if (data.code === 200) {
					var pages = getCurrentPages();
					uni.showToast({
						title: '收货成功'
					})
					setTimeout(() => {
						pages[pages.length - 2].$vm.search()
						uni.navigateBack()
					}, 1000)
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		// height: 100%;
		// overflow: auto;
		padding-bottom: 150upx;

		.customer-wrapper {
			background-color: #F26022;
			display: flex;
			align-items: center;
			color: #ffffff;
			padding: 30upx;
			border-radius: 0 0 20upx 20upx;
			font-size: 32rpx;

			.customer-info {
				flex: 1;
				border-right: 2upx solid #ffffff;

				.item {
					margin-bottom: 10upx;

					&:last-child {
						margin-bottom: 0upx;
					}
				}
			}

			.storage-number {
				margin-left: 50upx;
				text-align: center;
				font-weight: bold;
				font-size: 38upx;

				.date {
					font-weight: 300;
					font-size: 24rpx;
					color: #ffffff;
					margin-top: 10rpx;
				}
			}
		}

		.form-wrapper {
			background-color: #ffffff;
			border-radius: 20upx;
			margin: 20upx 20upx 0 20upx;
			padding-bottom: 14upx;
			overflow: hidden;

			.title-wrapper {
				height: 40upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 26upx 0;

				.title {
					color: #000000;
					font-weight: bold;
					font-size: 34upx;
					height: 34upx;
					line-height: 34upx;
					border-left: 10upx solid #F26022;
					padding-left: 18upx;

				}

				.edit {
					color: #F26022;
					font-size: 32upx;
					margin-right: 32upx;
				}
			}

			.item {
				display: flex;
				align-items: center;
				font-size: 32upx;
				line-height: 32upx;
				padding: 0 32upx;
				margin-bottom: 26upx;
				position: relative;

				.label {
					position: absolute;
					top: 0;
					color: #5B5B5B;
					margin-right: 30upx;
				}

				.value {
					color: #313131;
					flex: 1;
					text-align: right;
					margin-left: 180upx;

					.icon {
						position: relative;
						top: 2upx;
					}

					.placeholder {
						color: #999999;
					}

					.btn {
						color: #F26022;
						margin-left: 20upx;
					}
				}

				.item-tip {
					flex: 1;
					border-radius: 10upx;
					background-color: #f5f5f5;
					color: #FF5733;
					font-size: 28upx;
					padding: 20upx 20upx;
					line-height: 34upx;
				}
			}
		}

		.product-item {
			background-color: #ffffff;
			border-radius: 20upx;
			margin: 20upx 20upx 0 20upx;
			overflow: hidden;
			padding: 20upx;

			.product-name {
				display: flex;
				align-items: center;

				.line {
					min-width: 6upx;
					height: 30upx;
					background-color: #F26022;
					margin-right: 10upx;
				}

				.name {
					font-weight: bold;
					font-size: 32upx;
					color: #212121;
				}
			}

			.product-info {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.item {
					width: 50%;
					font-size: 28upx;
					color: #666666;
					margin-top: 14upx;
				}
			}

			.product-number {
				display: flex;
				align-items: center;
				margin: 20upx 0 0 0;
				border-top: 1upx dashed #efefef;
				padding-top: 20upx;
				font-size: 32upx;
				font-weight: bold;

				.input {
					flex: 1;
					text-align: center;
				}
			}

			.tip {
				display: flex;
				align-items: center;
				font-size: 28upx;
				color: #F26022;
				margin-top: 10upx;
			}
		}

		.btn-wrapper {
			position: fixed;
			height: 100upx;
			width: 100%;
			bottom: 0;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			z-index: 9;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 100%;
				background-color: #F26022;
				color: #ffffff;

				&.default {
					color: #232323;
					background-color: #ffffff;
				}
			}
		}

		.popup-wrapper {
			width: 600upx;
			background-color: #ffffff;
			border-radius: 20upx;
			min-height: 300upx;
			overflow: hidden;

			.title-wrapper {
				height: 100upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 32upx;
				font-size: 36upx;
				color: #2B2B2B;
			}

			.form-item {
				margin: 0 44upx;
				height: 100upx;
				display: flex;
				align-items: center;
				border-bottom: 2upx solid #f9f9f9;

				.label {
					font-size: 32upx;
					color: #5B5B5B;
				}

				.input-wrapper {
					height: 70upx;
					flex: 1;
					border: 1upx solid #dedede;
					border-radius: 10upx;
					margin-right: 15upx;

					.input {
						height: 70upx;
						width: 100%;
						text-align: center;
					}
				}
			}

			.popup-btn-wrapper {
				height: 150upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 40upx;

				.btn {
					width: 260upx;
					height: 76upx;
					border-radius: 40upx;
					background-color: #F26022;
					color: #ffffff;
					display: flex;
					align-items: center;
					justify-content: center;
					transition: .15s linear;

					&:active {
						background-color: #F2602266;
					}

					&.cancel {
						box-sizing: border-box;
						background-color: #ffffff;
						border: 2upx solid #F26022;
						margin-right: 40upx;
						color: #F26022;

						&:active {
							background-color: #F2F4FA77;
						}
					}
				}
			}
		}
	}
</style>